function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return year + "年" + month + "月" + day + "日";
}

function getTime(date) {
  let dataTime = ''
  let year = date.getFullYear()
  let month = date.getMonth()+1
  let format = (num) => num.toString().padStart(2, '0');
  let day = date.getDate() 
  dataTime = year+'-'+format(month)+'-'+format(day)
  return dataTime
}
const formatDay = dates => {
  let _day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
  let date = new Date(dates);
  date.setDate(date.getDate());
  let day = date.getDay();
  return _day[day];
}
const formatSole = () => {
  let timeNow = new Date();
  let hours = timeNow.getHours();
  let text = ``;
  if (hours >= 0 && hours <= 6) {
    text = `深夜了,不要熬夜太久哟`;
  } else if (hours > 6 && hours <= 8) {
    text = `早上好`;
  } else if (hours > 8 && hours <= 10) {
    text = `上午好`;
  } else if (hours > 10 && hours <= 13) {
    text = `中午好`;
  } else if (hours > 13 && hours <= 17) {
    text = `下午好`;
  } else if (hours > 17 && hours <= 23) {
    text = `晚上好`;
  }
  return text;
}

// 直接通过时间比较 
// // if (seconds1 > seconds2) {
//   console.log(`${time1} 晚于 ${time2}`);
// } else if (seconds1 < seconds2) {
//   console.log(`${time1} 早于 ${time2}`);
// } else {
//   console.log("时间相同");
// }
// const start = timeToSeconds("09:00:00"); // 32400 秒
// const end = timeToSeconds("18:00:00");   // 64800 秒
// const target = timeToSeconds("15:30:00");// 55800 秒

// if (target >= start && target <= end) {
//   console.log("时间在 09:00 至 18:00 之间");
// }
// 比较时间
const timeToSeconds = (timeStr) => {
    const [hours, minutes, seconds] = timeStr.split(':').map(Number); // 09:30:15
    return hours * 3600 + minutes * 60 + seconds;
}
// 时间加减法，并补零
const parseTimeToAdd = (timeStr, hoursAdd, minutesAdd, secondsAdd) => {
  let [hours, minutes, seconds] = timeStr.split(':').map(Number);

  // 增加秒数
  seconds += secondsAdd;
  if (seconds >= 60 || seconds < 0) {
    const deltaMinutes = Math.floor(seconds / 60);
    minutes += deltaMinutes;
    seconds = seconds % 60;
  }

  // 增加分钟数
  minutes += minutesAdd;
  if (minutes >= 60 || minutes < 0) {
    const deltaHours = Math.floor(minutes / 60);
    hours += deltaHours;
    minutes = minutes % 60;
  }

  // 增加小时数
  hours += hoursAdd;
  if (hours >= 24 || hours < 0) {
    hours = hours % 24;
  }

  // 格式化时间部分（补零）
  const format = (num) => num.toString().padStart(2, '0');
  const newTime = `${format(hours)}:${format(minutes)}:${format(seconds)}`;
  
  return newTime;
};

module.exports = {
  formatTime: formatTime,
  formatDay: formatDay,
  formatSole: formatSole,
  getTime:getTime,
  timeToSeconds:timeToSeconds,
  parseTimeToAdd:parseTimeToAdd
}